This is a demo of symbolic computation using Python and Sympy.


In [1]:
import numpy as np
import sympy
sympy.init_printing() # pretty printing

We first try to solve a complicated problem

Given: $$ y = \frac{x^2\sqrt{3x-2}}{(x+1)^2} $$

Find: $ \dot{y} $

Now we'll define symbolic variables


In [3]:
# must define symbolic variables before us
x, y = sympy.symbols('x y', real=True)

In [6]:
y = x**2*sympy.sqrt(3*x-2)/(x+1)**2
y # This causes it to output formatted nicely.


Out[6]:
$$\frac{x^{2} \sqrt{3 x - 2}}{\left(x + 1\right)^{2}}$$

In [7]:
# Taking the natural log of this:
a = sympy.ln(x**2*sympy.sqrt(3*x-2)/(x+1)**2)
a # Keep in mind that the left hand side is ln(y)


Out[7]:
$$\log{\left (\frac{x^{2} \sqrt{3 x - 2}}{\left(x + 1\right)^{2}} \right )}$$

In [8]:
# Taking its derivative with respect to x
# So, we took the log of the right side, then the derivative
sympy.diff(a,x)


Out[8]:
$$\frac{\left(x + 1\right)^{2}}{x^{2} \sqrt{3 x - 2}} \left(\frac{3 x^{2}}{2 \left(x + 1\right)^{2} \sqrt{3 x - 2}} - \frac{2 x^{2} \sqrt{3 x - 2}}{\left(x + 1\right)^{3}} + \frac{2 x \sqrt{3 x - 2}}{\left(x + 1\right)^{2}}\right)$$

In [10]:
# That is kind of messy. Let the computer simplify it
sympy.simplify(sympy.diff(a,x))


Out[10]:
$$\frac{3 x^{2} + 15 x - 8}{2 x \left(3 x^{2} + x - 2\right)}$$

In [11]:
# We could have done this all at once
sympy.simplify(sympy.diff(sympy.ln(y),x))


Out[11]:
$$\frac{3 x^{2} + 15 x - 8}{2 x \left(3 x^{2} + x - 2\right)}$$

Lets transform the left hand side in teh same way

$$ y $$

Take the natural log

$$ ln (y)$$

Take the derivative

$$ \frac{1}{y}dy $$

Now the complete equation is as follows

$$ \frac{1}{y} dy = \frac{3x^2 + 15x - 8}{2x(3x^2 + x -2)} $$

The right hand side is given above, so to find the solution just multiply $ y $


In [12]:
sympy.simplify(sympy.diff(sympy.ln(y), x)*y)


Out[12]:
$$\frac{x \left(3 x^{2} + 15 x - 8\right)}{2 \sqrt{3 x - 2} \left(x^{3} + 3 x^{2} + 3 x + 1\right)}$$

We can simplify this as well and just take the derivative directly


In [13]:
sympy.simplify(sympy.diff(y, x))


Out[13]:
$$\frac{x \left(3 x^{2} + 15 x - 8\right)}{2 \sqrt{3 x - 2} \left(x^{3} + 3 x^{2} + 3 x + 1\right)}$$

This was a round about way to do something simple, but we can compare them directly to make sure


In [14]:
sympy.simplify(sympy.diff(y, x)) == sympy.simplify(sympy.diff(sympy.ln(y),x)*y)


Out[14]:
True

Or we can plot it


In [15]:
sympy.plot((sympy.diff(y, x), (x, 0.2, 10)), (y, (x, 0.5, 10)))


Out[15]:
<sympy.plotting.plot.Plot at 0x7ff403a937f0>

In [17]:
# To change colors
# show = False delays the plot until we can set all of the parameters
# legend turns on the legend and uses the labels we have later.
p = sympy.plot((sympy.diff(y,x),(x,0.2,10)), (y, (x,0.5,10)), show = False, legend = True)
p[0].line_color = 'blue'
p[0].label = '$\\frac{dy}{dx}$'
p[1].line_color = 'green'
p[1].label = '$y$'
p.show()


Can make a substitution to evaluate the function


In [19]:
from sympy.abc import alpha
y.subs(x,alpha)


Out[19]:
$$\frac{\alpha^{2} \sqrt{3 \alpha - 2}}{\left(\alpha + 1\right)^{2}}$$